package firstping.controller;

import java.io.IOException;
import java.util.List;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import firstping.data.QnAPersistenceManager;
import firstping.model.Customer;

@SuppressWarnings("serial")
public class ChangePasswordServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().println("Hello, world");
		
		HttpSession session = req.getSession();
		String customer = (String) session.getAttribute("customer");
		
		if(customer != null)
		{
			String[] check = new String[4];
			req.setAttribute("check", check);
			
			try {
				req.getRequestDispatcher("/change_password.jsp").forward(req, resp);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			resp.sendRedirect("/");
		}
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().println("Change password success");
		
		PersistenceManager psm = QnAPersistenceManager.get().getPersistenceManager();
		
		HttpSession session = req.getSession();
		String customer = (String) session.getAttribute("customer");
		
		if(customer != null)
		{
			String old_pwd     = req.getParameter("old_pwd");
			String new_pwd  = req.getParameter("new_pwd");
			String re_pwd = req.getParameter("re_pwd");
			
			String[] check = new String[4];
			
			
			if(old_pwd == null || (old_pwd != null && !old_pwd.equals(session.getAttribute("pwd"))))
			{
				check[0] = "Password incorrect";
			} else {
				if(new_pwd == null || (new_pwd != null && new_pwd.equals("")))
				{
					check[1] = "New password is not empty";
				} else {
					if(!new_pwd.equals(re_pwd))
					{
						check[2] = "Confirm password does not match";
					}
				}
			}
			
			if(check[0] == null && check[1] == null && check[2] == null && check[3] == null)
			{
				session.setAttribute("pwd", new_pwd);
				
				Query query = psm.newQuery(Customer.class);
		    	query.setFilter("email=='"+customer+"'");
		    	@SuppressWarnings("unchecked")
				List<Customer> cus = (List<Customer>) query.execute();
		    	if(cus.size() > 0)
		    	{
		    		cus.get(0).setPassword(new_pwd);
		    		
					psm=JDOHelper.getPersistenceManager(cus.get(0));
					psm.currentTransaction().begin();
					psm.makePersistent(cus.get(0));
					psm.currentTransaction().commit();
					
					req.setAttribute("success", "Change password successfully .");
		    	}
			}
		    req.setAttribute("check", check);
		
			try {
				req.getRequestDispatcher("/change_password.jsp").forward(req, resp);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
		} else {
			resp.sendRedirect("/");
		}
	}
}
